package group09.android.locationtracking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Iface {

	
	private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
 
        String line = null;
        String errCode = "";
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            //e.printStackTrace();
        	errCode = "90";
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                //e.printStackTrace();
            	errCode = "91";
            }
        }
        if (errCode == "")
        	return sb.toString();
        else
        	return errCode;
    }
	
	public String connect(String url){
    
		String result = "";
		HttpClient httpclient = new DefaultHttpClient();
		// Prepare a request object
        HttpGet httpget = new HttpGet(url);
        
        HttpResponse response;
        
        try {
        	// Execute the request
            response = httpclient.execute(httpget);
            
         // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
 
            if (entity != null) {
 
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                
                //System.out.print("result: " + result);
                //Log.i("Praeda",result);
                
                /*
                // A Simple JSONObject Creation
                JSONObject json=new JSONObject(result);
                Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
 
                // A Simple JSONObject Parsing
                JSONArray nameArray=json.names();
                JSONArray valArray=json.toJSONArray(nameArray);
                for(int i=0;i<valArray.length();i++)
                {
                    Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"
                            +"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
                }
 
                // A Simple JSONObject Value Pushing
                json.put("sample key", "sample value");
                Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");
                */
                
                // Closing the input stream will trigger connection release
                instream.close();
            }
            
            
            // Examine the response status
        } catch (ClientProtocolException e) {
            //e.printStackTrace();
        	result = "92";
        } catch (IOException e) {
            //e.printStackTrace();
        	result = "93";
		} catch (Exception e) {
	        //e.printStackTrace();
	    	result = "910";
	    }/*catch (JSONException e) {
            e.printStackTrace();
        }*/
        
        return result;
	}
	
}
